package persistences;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import dto.TrainingPlanDTO;
import util.DBUtil;

public class TrainingPlanDAO {
	private DBUtil dbUtil;
	private Connection conn;

	public TrainingPlanDAO() {
		dbUtil = new DBUtil();
		conn = dbUtil.getConnection();
	}

	public boolean findTrainingPlan(String id, String name, String planYear, String startDate, String endDate,
			String majorPlane) {
		boolean result = false;
		QueryRunner qr = new QueryRunner();
		TrainingPlanDTO  TrainingPlanDTO = null;
		
		String sql ="select id,name,planYear,startDate,endDate,majorPlane from t_base_user_info where id=? and name=? and planYear=? and startDate=? and endDate=? and majorPlane=?";
		String params[] = new String[6];
		params[0] = id;
		params[1] = name;
		params[2] = planYear;
		params[3] = startDate;
		params[4] = endDate;
		params[5] = majorPlane;
		
		try{
			TrainingPlanDTO = qr.query(conn, sql, new BeanHandler<TrainingPlanDTO>(TrainingPlanDTO.class),params);	
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		if (TrainingPlanDTO != null){
			result = true;
			System.out.println(TrainingPlanDTO.getid()+"  "+TrainingPlanDTO.getname()+"  "+TrainingPlanDTO.getplanYear()+"  "+TrainingPlanDTO.getstartDate()+"  "+TrainingPlanDTO.getendDate()+"  "+TrainingPlanDTO.getmajorPlane());
		}
		
		return result;
	}

}
